import defineReactive from "./defineReactive";
import { def } from '../utils/utils'
import { arrayMethods } from './array'
import Dep from "./Dep";
export function observe(value) {

    // 判断是不是对象
    if(typeof value !=='object') return

    let ob ;
    if(value.__ob__){
        ob = value.__ob__
    } else {
        ob = new Observer(value)
    }

    return ob;
}

class Observer {
    constructor(value){
        this.dep = new Dep()
        def(value,'__ob__',this,false)
        if(Array.isArray(value)){
            Object.setPrototypeOf(value,arrayMethods)
            this.observeArray(value)
        } else {
            this.walk(value)
        }
    }
    walk(value){
        for (let k in value) {
            defineReactive(value,k)
        }
    }
    observeArray(arr){
        for (let i = 0; i < arr.length; i++) {
            observe(arr[i])
        }
    }
}